package com.lzz.bottomencapsulation.utils;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * sd卡相关
 */
public class SDCardUtils {
    private SDCardUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * sd卡是否存在
     */
    public static boolean isSDCardEnable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

    }

    /**
     * sd 根目录
     */
    public static String getSDCardPath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
    }

    /**
     * 获取手机内部可用空间大小
     */
    static public long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

    /**
     * 获取手机内部空间大小
     */
    static public long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();// Gets the Android data
        // directory
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize(); // 每个block 占字节数
        long totalBlocks = stat.getBlockCount(); // block总数
        return totalBlocks * blockSize;
    }

    /**
     * 获取手机外部可用空间大小
     */
    static public long getAvailableExternalMemorySize() {
        if (isSDCardEnable()) {
            File path = Environment.getExternalStorageDirectory();// 获取SDCard根目录
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return -1;
        }
    }

    /**
     * 获取手机外部总空间大小
     */
    static public long getTotalExternalMemorySize() {
        if (isSDCardEnable()) {
            StatFs stat = new StatFs(getSDCardPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return totalBlocks * blockSize;
        } else {
            return -1;
        }
    }

    /**
     * 获取system路径
     */
    public static String getRootDirectoryPath() {
        return Environment.getRootDirectory().getAbsolutePath();
    }

}
